package com.train.servlet;

import com.alibaba.fastjson.JSONObject;
import com.train.pojo.User;
import com.train.service.UserService;
import com.train.service.UserServiceImpI;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import redis.clients.jedis.Jedis;

import java.io.IOException;

@WebServlet("/login")
public class LoginSelect extends HttpServlet {

    private UserService userService=new UserServiceImpI();
//    private DaoUser du = new DaoUser();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取Redis对象
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("用户名="+username);
        System.out.println("密码="+password);
        //获取Session对象
        HttpSession session = request.getSession();
        JSONObject json = new JSONObject();

        if(username==null || username==""){
            String phone = request.getParameter("phone");
            String code = request.getParameter("code");
            System.out.println(phone);
            System.out.println("code"+code);
            //去Redis中，更具phone这个键来获取code（验证码）
            String code1=jedis.get(phone);
            System.out.println("code1"+code1);

            if(code.equals(code1)){
                json.put("code",200);
                json.put("msg","OK");
                session.setAttribute("user",phone);
            }else {
                json.put("code",300);
                json.put("msg","error");
            }
        }else {
            //通过前端传过来的用户名，查询用户是否存在
            User user= null;
            try {
                user = userService.login(username);
                System.out.println(request.getContextPath());
                if(user!=null){
                    if(user.getPassword().equals(password)){
                        System.out.println("登陆成功");
                        json.put("code",200);
                        json.put("msg","登陆成功");
                        //将user这个信息保存在Session对象中
                        session.setAttribute("user",user);

                        /**----------------实现免登录------------------*/
              /*      String f = request.getParameter("f");
                    if("1".equals(f)){
                        //创建Cookie对象，存储用户与密码
                        Cookie cookie1 = new Cookie("username",username);
                        Cookie cookie2 = new Cookie("password",password);
                        //设置Cookie的有效期
                        cookie1.setMaxAge(60*60);
                        cookie2.setMaxAge(60*60);
                        //设置Cookie的path（只要访问这个应用，浏览器一定会携带cookie）
                          cookie1.setPath(request.getContextPath());
                          cookie2.setPath(request.getContextPath());
                        //响应cookie给浏览器
                        response.addCookie(cookie1);
                        response.addCookie(cookie2);

                    }*/
                    }else{
                        System.out.println("密码错误");
                        json.put("code",300);
                        json.put("msg","密码错误");
                    }
                }else{
                    System.out.println("用户名不存在");
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }

        }


        // 解决响应乱码
//        response.setContentType("text/html;charset=utf8");
        // 响应给前端一个字符串
        response.getWriter().write(json.toString());
    }
}
